#!/usr/bin/env python
# -*- coding: utf-8 -*-
from . import db
import os
import shutil
from werkzeug.security import generate_password_hash, check_password_hash
from flask import current_app, request
from datetime import datetime
from app.exceptions import ValidationError

from flask.ext.login import UserMixin
from . import login_manger
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
import hashlib
from flask.ext.login import AnonymousUserMixin
from datetime import  datetime
from sqlalchemy import desc
import base64
from . lib import util
import uuid
from exceptions import ConfigFileNotExist
import time


@login_manger.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


class User(UserMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    # role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    password_hash = db.Column(db.String(128))
    name = db.Column(db.String(64))
    member_snice = db.Column(db.DateTime(), default=datetime.utcnow)
    last_login = db.Column(db.DateTime(), default=datetime.utcnow)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)

    def ping(self):
        self.last_login = datetime.utcnow()
        db.session.add(self)
        db.session.commit()

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    def generate_confirmation_token(self, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id})

    def generate_reset_token(self, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'reset': self.id})

    def generate_auth_token(self, expiration):
        s = Serializer(current_app.config['SECRET_KEY'],
                       expires_in=expiration)
        return s.dumps({'id': self.id})

    @staticmethod
    def verfiy_auth_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return None
        return User.query.get(data['id'])

    def __repr__(self):
        return '<User %r>' % self.username


class Host(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    hostname = db.Column(db.String(256), primary_key=True)
    main_ip = db.Column(db.String(128), unique=True)
    SN = db.Column(db.String(32), unique=True)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    remark = db.Column(db.Text)

class Board(db.Model):
    pass

class CPU(db.Model):
    pass

class Memory(db.Model):
    pass

class Disk(db.Model):
    pass

class Nic(db.Model):
    pass

class OS(db.Model):
    pass

class IP(db.Model):
    pass

class NetWork(db.Model):
    pass




